#!/bin/bash

# ----------------------------------------------------------------------
# Filename:   dns_conf.sh
# Version:    1.0
# Date:       2013/10/16
# Author:     huijing.hei
# Email:      huijing.hei@cs2c.com.cn
# Summary:    02系统基本功能-04网络服务-06DNS-01字符模式-02DNS服务常规配置-01字符方式-01DNS基本服务器配置
# Notes:       ***
# Copyright:    China Standard Software Co., Ltd.
# History：     
#             Version 1.0, 2013/10/16
#             - Start named service, setup named.conf
#             Version 1.1,2014/10/30
#             - The second one
#             - change named.conf file for mips  line 71,97;
# ----------------------------------------------------------------------

##! @TODO: start&stop named service
##! @AUTHOR: huijing.hei
##! @VERSION: 1.0 
##! @OUT: 0 => success; 1 => failure

function do_setup()
{
    serv=named
    CONF="/etc/named.conf"
    DNS_SERVER=`ifconfig | grep 'inet ' | grep -m 1 -v '127.0.0.1'  | awk '{print $2}' | tr -d "addr:"`
    [ -z ${DNS_SERVER} ] && { echo "DNS SERVER ip is null"; exit 1; }

    # parse dns server ip for named.conf
    DNS_SERVER_1=`echo ${DNS_SERVER} | awk -F "." '{print $1}'`
    DNS_SERVER_2=`echo ${DNS_SERVER} | awk -F "." '{print $2}'`
    DNS_SERVER_3=`echo ${DNS_SERVER} | awk -F "." '{print $3}'`
    DNS_SERVER_4=`echo ${DNS_SERVER} | awk -F "." '{print $4}'`
    [ -z ${DNS_SERVER_1} -o -z ${DNS_SERVER_2} -o -z ${DNS_SERVER_3} -o -z ${DNS_SERVER_4} ] && { echo "DNS SERVER ip is wrong, pls check"; exit 1; }

    # check owner of /etc/named.conf
    OWNER=`ls -l ${CONF} | awk '{print $3,$4}'`
    if [ "${OWNER}" != "named named" ]; then
        chown named:named ${CONF}
        EchoResult "check owner of ${CONF}"
    fi

    # backup /etc/named.conf
    mv ${CONF} ${CONF}.bak
    EchoResult "backup ${CONF}"
    
    # update named.conf
    TEST_COM="test${DNS_SERVER_4}.com1"

    cat << EOF > ${CONF}
options {
	listen-on port 53 { ${DNS_SERVER}; };
	listen-on-v6 port 53 { ::1; };
	directory 	"/var/named";
	dump-file 	"/var/named/data/cache_dump.db";
        statistics-file "/var/named/data/named_stats.txt";
        memstatistics-file "/var/named/data/named_mem_stats.txt";
	allow-query     { any; };
	recursion yes;

	dnssec-enable yes;
	dnssec-validation yes;
	dnssec-lookaside auto;

	/* Path to ISC DLV key */
	bindkeys-file "/etc/named.iscdlv.key";

#	managed-keys-directory "/var/named/dynamic";
};

logging {
        channel default_debug {
                file "data/named.run";
                severity dynamic;
        };
};

zone "." IN {
	type hint;
	file "named.ca";
};

zone "${TEST_COM}" IN {
type master;
file "${TEST_COM}.zone";
};
zone "${DNS_SERVER_3}.${DNS_SERVER_2}.${DNS_SERVER_1}.in-addr.arpa" IN {
type master;
file "zone.${TEST_COM}";
};

include "/etc/named.rfc1912.zones";
#include "/etc/named.root.key";
EOF

    # create test.com.zone, zone.test.com
    PATH_TEST_COM=/var/named/
    [ -f "${PATH_TEST_COM}${TEST_COM}.zone" ] && rm -f "${PATH_TEST_COM}${TEST_COM}.zone"
    [ -f "${PATH_TEST_COM}zone.${TEST_COM}" ] && rm -f "${PATH_TEST_COM}zone.${TEST_COM}"
    cat << EOF > ${PATH_TEST_COM}${TEST_COM}.zone
\$TTL 86400
@ IN SOA ${TEST_COM}. root.${TEST_COM}. (
12 ; serial
28800 ; refresh
7200 ; retry
3600000 ; expire
8400 ) ; minimum
@      IN NS www.
www IN A ${DNS_SERVER}
EOF

    cat << EOF > ${PATH_TEST_COM}zone.${TEST_COM}
\$TTL 86400
@ IN SOA ${TEST_COM}. root.${TEST_COM}. (
2012121100 ; serial
28800 ; refresh
7200 ; retry
3600000 ; expire
8400 ) ; minimum
@    IN NS ${TEST_COM}.
${DNS_SERVER_4} IN PTR www.${TEST_COM}.
EOF

    restart_daemon ${serv}

    sleep 3
    # check httpd start
    status_daemon ${serv}  && netstat -nlutp | grep ${serv}
    EchoResult "service ${serv} start"

    trap do_cleanup INT EXIT
}

##! @TODO:  test dns
##! @AUTHOR: huijing.hei
##! @VERSION: 1.0 
##! @OUT: 0 => success; 1 => failure
function do_test()
{
    #########################
    # nslookup domain
    RESULT=`nslookup www.${TEST_COM} ${DNS_SERVER} | grep -iE 'name|address' | grep -v 53`

    sleep 3
    
    RESULT_NAME=`echo "${RESULT}" | grep -i 'name' | awk '{print $2}'`
    RESULT_ADDRESS=`echo "${RESULT}" | grep -i 'address' | awk '{print $2}'`

    [ "${RESULT_NAME}" = "www.${TEST_COM}" -a "${RESULT_ADDRESS}" = "${DNS_SERVER}" ]
    EchoResult "test dns server with base config, nslookup domain"


    ##########################
    # nslookup ip
    nslookup ${DNS_SERVER} ${DNS_SERVER} | grep -i "${DNS_SERVER_4}.${DNS_SERVER_3}.${DNS_SERVER_2}.${DNS_SERVER_1}.in-addr.arpa[[:space:]]*name = www.${TEST_COM}."
    EchoResult "test dns server with base config, nslookup ip"
}

##! @TODO:  cleanup tmp files, back up named.conf and stop named service.
##! @AUTHOR: huijing.hei
##! @VERSION: 1.0 
##! @OUT: 0 => success; 1 => failure
function do_cleanup()
{
    stop_daemon ${serv}
    rm -f "${PATH_TEST_COM}${TEST_COM}.zone" "${PATH_TEST_COM}zone.${TEST_COM}"
    [ -f ${CONF}.bak ] && mv -f ${CONF}.bak ${CONF}

}
# include lib files

if [ -z "$SFROOT" ]
then
    CMDLIB=./
    SFROOT=./
else
    CMDLIB=${SFROOT}/testcases/Network_Service
fi

. ${SFROOT}/lib/Echo.sh
. ${CMDLIB}/lib/Net.sh
do_setup

do_test || EchoResult "testcase: ${serv} setup dns"
echo ""
